package com.devshephard.spinLock;

import java.util.concurrent.atomic.AtomicReference;

public class MyLock {

    AtomicReference<Thread> threadAtomicReference = new AtomicReference<>();

    public void getLock(){

        Thread currentThread = Thread.currentThread();

        System.out.println("线程：" + currentThread.getName() + "获取锁");

        do{

        }while (!threadAtomicReference.compareAndSet(null,currentThread));

        System.out.println("线程：" + currentThread.getName() + "正在使用锁");

    }

    public void unLock(){

        Thread currentThread = Thread.currentThread();

        System.out.println("线程：" + currentThread.getName() + "释放锁");

        threadAtomicReference.compareAndSet(currentThread,null);


    }

}
